Skip to content

Integrate upstream CPU kernel fixes (reductions, binary ops, conv, FP8, fp16 masks, float64 math) - #48

Merged
jjang-ai merged 10 commits into
osaurus-ai:mainfrom
rcfa:apple/cpu-kernels
Nov 22, 2026
Merged

jjang-ai merged 10 commits into
osaurus-ai:mainfrom
rcfa:apple/cpu-kernels

Conversation

@rcfa

@rcfa rcfa commented Nov 5, 2026

Copy link
Copy Markdown

Branch apple/cpu-kernels, 10 commits on origin/main 3b39b5e.

Bring in ten bug fixes from ml-explore/mlx main that the fork does not have yet. The code is Apple's and its
contributors': each commit is a git cherry-pick -x of the upstream commit, with the upstream author kept and
the (cherry picked from commit …) line recording provenance. Nothing of ours is in these commits.

Why we push them: osaurus and vmlx-swift ship from this fork, and these are wrong-result or memory-safety bugs on
the CPU backend, which vmlx-swift also runs on Linux. Each one is still present on origin/main: the fork has not
changed these functions since the merge base 64ea011, and each upstream change applies to them unchanged.

Included, in upstream order:

Upstream Commit Here What it fixes
ml-explore#4549 0840c42 cdd6a39 CPU reductions kept offsets, sizes and the nd_loop callback in int, so inputs past 2^31 elements read the wrong rows or out of bounds; the Metal row reductions now pick the 64-bit kernel when data_size() (not only size()) passes 2^31.
ml-explore#4593 1bed2e2 66902c4 complex64 to bool looked at the real part only (copy kernels), and all/any tested complex64 as raw int64 bits, so -0+0j counted as true; adds an explicit complex64_t::operator bool, a complex64 and/or reduce on Metal, and the CUDA cast (&& → ||, True → 1+0j).
ml-explore#4602 8c56dd8 994c9a3 NEON float16 comparison and ! masks were built from *(uint16_t*)&out, which reads lane 0 and broadcasts it to every lane; now loads all lanes (arm64 CPU, macOS and Linux).
ml-explore#4599 017e1c3 94a2836 Dynamic slicing read non-contiguous start indices as if contiguous (CPU, Metal, CUDA); now copies them contiguous first.
ml-explore#4638 53fbe56 bc12f2a The CPU JIT compile cache wrote the library in place, so a second process could load a half-written .so; now builds under a unique name and renames into place.
ml-explore#4651 0d8236f 6c6c971 Missing break after the bool_ case in binary_int_op_cpu: bool inputs fell through and also ran the uint8 kernel.
ml-explore#4648 8d5bdbc 97d2529 binary_op_dispatch_dims took the element count as int (truncated on large arrays).
ml-explore#4668 af9a592 cdded15 The direct (slow) CPU conv did not clamp its border bounds to the output size, so padding larger than the output wrote past the output buffer (2D and 3D).
ml-explore#4756 7d2d94a dbac850 to_fp8 turned NaN into the saturated max (0x7E) instead of NaN (0x7F), and an E8M0 scale of 0xFF decoded to +inf instead of NaN (CPU and Metal fp8.h).
ml-explore#4789 a8f66b5 a32e8e3 float64 exp, sin, cos and erf on the CPU were computed through the float32 SIMD path.

Not included here: the other core/CPU/io fixes are in apple/array-views, apple/transforms-ops and apple/io.
Left out of the whole intake: ml-explore#4694 (shift fix, already our #41) and ml-explore#4634 (foreign buffer ownership: it changes
allocator::malloc to return allocator::Data and removes array(Buffer, …, Deleter) and allocator::free, which
the fork's mmap safetensors loader uses; listed for review by the fork's maintainer).

Conflict resolutions

Library code applied without conflicts. Two test files:

Overlaps with open PRs

Validation

Compared with main (3b39b5e), built and run the same way:

  • Linux arm64 (Debian in a VM on an M4 Max), C++ suite on the CPU, GCC 12 Debug and clang 21 Debug with UBSan:
    the same failing cases as main and no UBSan report that main lacks. On main GCC fails only test one-bit affine endpoints CPU GPU and small-K dispatch (needs a GPU), and clang the same plus 16 compile/fusion cases whose CPU
    JIT fails in that VM.

  • macOS (M4 Max, Apple clang 21), C++ suite, Release, on the CPU and on Metal.

  • Python (macOS, Python 3.14, the extension in Release with Metal), on the GPU and with DEVICE=cpu: the test files
    this PR touches, with this branch and with main. Each upstream test method this PR adds or changes also ran alone
    against main's build, with this branch's version of its file.

  • Linux: 285 cases (main 284).

  • macOS: 321 cases (main 320), all pass on the CPU and on Metal (main fails test concurrent allocator statistics
    on Metal in that run; it fails intermittently).

  • Python, test_conv, test_double, test_ops, test_quantized, test_reduce: the failing subtests of main are
    unchanged (test_qmm_mid_m and test_qmm_splitk_precision precision thresholds on the GPU; tests that need a GPU
    stream under DEVICE=cpu), and the new methods pass on both devices. The upstream methods against main
    (GPU / CPU):

🤖 Generated with Claude Code

nicoloangileri and others added 10 commits November 4, 2026 16:40
Co-authored-by: Cheng <git@zcbenz.com>
(cherry picked from commit 0840c42)
Co-authored-by: Cheng <git@zcbenz.com>
(cherry picked from commit 8c56dd8)
Co-authored-by: Cheng <git@zcbenz.com>
(cherry picked from commit 53fbe56)
Co-authored-by: Cheng <git@zcbenz.com>
(cherry picked from commit af9a592)
…explore#4756)

Co-authored-by: Cheng <git@zcbenz.com>
(cherry picked from commit 7d2d94a)
rcfa added a commit to rcfa/mlx that referenced this pull request Nov 8, 2026
The general reduction's cascade loop computed the input offset and the
element count in int, so a reduction over 2^31 or more elements wrapped.
Apple's 64-bit reduce offsets (in osaurus-ai#48) do the same for the other loops.
@jjang-ai
jjang-ai merged commit 3fc42eb into osaurus-ai:main Nov 22, 2026
rcfa added a commit to cubiculum-ai/mlx that referenced this pull request Nov 22, 2026
Conflicts, all where main's CPU fixes meet this PR's Highway code:
- cpu/reduce.cpp: strided_reduce (osaurus-ai#34's cascade) is common to both builds;
  the Highway contiguous_reduce sums in blocks of sum_block, as osaurus-ai#34 does.
  The non-Highway code is main's.
- cpu/softmax.cpp: the Highway row kernel sums in blocks of sum_block, as
  osaurus-ai#34 does. The non-Highway code is main's.
- cpu/binary.h: main's size_t loop (osaurus-ai#48) under this PR's guard.
- cpu/simd/base_simd.h, tests/blas_tests.cpp, tests/ops_tests.cpp: both
  sides kept.

Check: with MLX_USE_HIGHWAY_KERNELS undefined (unifdef), every source file
reduces to main's version, except one comment in cpu/quantized.cpp that
this PR already had. The tests are the union of main's and this PR's.
@rcfa
rcfa deleted the apple/cpu-kernels branch November 23, 2026 00:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants